#include "CBugky.h"


CBugky::CBugky()
{
	m_CurrentState		= ANIMATION_STATE_STAND;
	m_fActionTime		= 0.0f;
	m_fMaxTime			= 0.0f;
	m_fInvulnerableTime	= 0.0f;
	m_fMaxInvulTime		= 0.3f;
	m_fAlpha			= 255.0f;
	SetDirection( CMob::DIR_LEFT );
}

CBugky::CBugky(ULONG ID , float Xpos , float Ypos , float deep , float XVelocity , float YVelocity , int width , int height): CMob( ID , Xpos , Ypos , deep , XVelocity , YVelocity , width , height )
{
	m_CurrentState		= ANIMATION_STATE_STAND;
	m_fActionTime		= 0.0f;
	m_fMaxTime			= 0.0f;
	m_fInvulnerableTime	= 0.0f;
	m_fMaxInvulTime		= 0.3f;
	m_fAlpha			= 255.0f;
	SetDirection( CMob::DIR_LEFT );
}


CBugky::~CBugky()
{

}




//--------------------------------------//
// Name : Init							//
// Desc : Init Attribute for this mob	//
//--------------------------------------//
void CBugky::Init(LPCTSTR DefineFile)
{
	char	Buffer[1025];
	char	Section[100];
	float	x , y , z;
	float	vx , vy;
	int		width , height;
	float	offsetX , offsetY;
	int		offsetWidth , offsetHeight;
	int		imageIndex;

	strcpy( Section , "BugMob" );

	//Get positiom
	GetPrivateProfileString( Section , "Position" , "0, 0, 0" , Buffer , 1024 , DefineFile );
	sscanf( Buffer , "%f,%f,%f" , &x , &y , &z );

	SetPosition(x , y);
	SetDeep(z);

	//Get velocity
	GetPrivateProfileString( Section , "Velocity" , "0.0, 0.0" , Buffer , 1024 , DefineFile );
	sscanf( Buffer , "%f,%f" , &vx , &vy );

	SetVelocity( vx , vy );

	//Get size
	GetPrivateProfileString( Section , "Size" , "0, 0" , Buffer , 1024 , DefineFile );
	sscanf( Buffer , "%i,%i" , &width , &height );

	SetSize( width , height );

	//Get bounding box information
	GetPrivateProfileString( Section , "BoundBox" , "0.0, 0.0, 0, 0 " , Buffer , 1024 , DefineFile );
	sscanf( Buffer , "%f,%f,%i,%i" , &offsetX , &offsetY , &offsetWidth , &offsetHeight );

	CreateBoundBox( offsetX , offsetY , offsetWidth , offsetHeight );

	//Get attribute information
	SetMaxAttribute( GetPrivateProfileInt( Section , "MaxHp" , 0 , DefineFile ) , GetPrivateProfileInt( Section , "MaxMp" , 0 , DefineFile ) );
	SetCurrAttribute( GetMaxHp() , GetMaxMp() );
	SetDamage( GetPrivateProfileInt( Section , "MinDamage" , 0 , DefineFile ) , GetPrivateProfileInt( Section , "MaxDamage" , 0 , DefineFile ) );
	SetLv( GetPrivateProfileInt( Section , "Level" , 0 , DefineFile ) );

	//Get Image index
	imageIndex = GetPrivateProfileInt( Section , "ImageIndex" , 0 , DefineFile );


	//----------------------------------//
	//	Get the Animation information	//
	//----------------------------------//
	strcpy( Section , "BugkyAnimation" );
	int numFrame = GetPrivateProfileInt( Section , "NumFrame" , 0 , DefineFile );
	int numFrameInRow = GetPrivateProfileInt( Section , "NumFrameInRow" , 0 , DefineFile );

	LoadSprite( &CResourceManager::GetInstance()->m_pListImage[imageIndex] , numFrame , numFrameInRow );

	int animCount = GetPrivateProfileInt( Section , "AnimCount" , 0 , DefineFile );
	int index , start , end , loop;
	float speedTop , speedBot;
	char keyName[100];
	CAnimation* pAnim = new CAnimation[animCount];

	for( ULONG i = 0 ; i < animCount ; ++i)
	{
		_stprintf( keyName , _T("Anim_%i") , i );
		GetPrivateProfileString( Section , keyName , "0, 0, 0, 0, 0.0/1.0" , Buffer , 1024 , DefineFile );
		sscanf( Buffer , "%i,%i,%i,%i,%f/%f" , &index , &start , &end , &loop , &speedTop , &speedBot );
		pAnim[i].CreateAnimation( index , start , end , loop , speedTop / speedBot );
	}

	GetSprite()->SetAnimation(pAnim);
	GetSprite()->SetAnimationIndex( ANIMATION_STATE_STAND );

	m_pWeapon = new WEAPON( "Dark_Sword" , 20 , 30 );

	m_pArmor = new ARMOR( "Dark_Armor" , 0 );

}


//------------------------------------------//
// Name : SetState							//
// Desc : Set current state for this mob	//
//------------------------------------------//
void CBugky::SetState(CBugky::ANIMATION_STATE state)
{
	m_CurrentState = state;
}



//--------------------------------------//
// Name : GetState						//
// Desc : Get current state of this mob	//
//--------------------------------------//
CBugky::ANIMATION_STATE CBugky::GetState()
{
	return m_CurrentState;
}



//--------------------------------------//
// Name : Hurt							//
// Desc : Set this mob into hurt state	//
//--------------------------------------//
void CBugky::Hurt(CCharacter *pCharacter)
{
	int damgRecive = pCharacter->DamageCal();
	int defend = GetDef() + m_pArmor->defend;

	this->ReduceHp( damgRecive - defend );

	SetColliable(false);
}



//------------------------------------------//
// Name : UpdateHurt						//
// Desc : Update when this mob is hurting	//
//------------------------------------------//
void CBugky::UpdateHurt(float deltaTime)
{
	static bool flag = false;
	if( !IsColliable() )
	{
		if( isDying() )
		{
			m_fAlpha -= 10.0f;
			if( m_fAlpha <= 0 )
			{
				SetAlive( false );
			}
			GetSprite()->SetColorAlpha( m_fAlpha );
		}
		else
		{
			m_fInvulnerableTime+= deltaTime;
			if( m_fInvulnerableTime >= m_fMaxInvulTime )
			{
				SetColliable();
				GetSprite()->SetColorAlpha( 255 );
				m_fInvulnerableTime = 0.0f;
				m_fAlpha = 255.0f;
				flag = false;
				return;
			}
			if( flag )
			{
				m_fAlpha += 10;
				if( m_fAlpha >= 255 )
				{
					flag = false;
				}
			}
			else
			{
				m_fAlpha -= 10;
				if( m_fAlpha <= 0 )
				{
					flag = true;
				}
			}
			GetSprite()->SetColorAlpha( m_fAlpha );
		}
	}
}



//--------------------------//
// Name : Update			//
// Desc : Update this mob	//
//--------------------------//
void CBugky::Update(float deltaTime)
{
	srand( timeGetTime( ) );
	float moveX = 0.0f;
	RANGE moveRange;

	// Retrieve the movement range of this mob
	GetMovementRange( &moveRange );

	// Get all callback function
	COLLISIONCHECKING collisionCallbacks = (COLLISIONCHECKING)m_CallBack[CALLBACK_TYPE_COLLISION].pFunction;
	ACTIVE	activeCallbacks	= (ACTIVE)m_CallBack[CALLBACK_TYPE_ACTIVE].pFunction;

	if( m_fActionTime == 0.0f )
	{
		m_fMaxTime = rand() % 3;
	}

	m_fActionTime += deltaTime;
	if( m_fActionTime >= m_fMaxTime )
	{
		m_fActionTime = 0.0f;
		if( m_CurrentState == ANIMATION_STATE_STAND )
		{
			m_CurrentState = ANIMATION_STATE_MOVE;
			GetSprite()->SetAnimationIndex( m_CurrentState );
		}
		else if( m_CurrentState == ANIMATION_STATE_MOVE )
		{
			m_CurrentState = ANIMATION_STATE_STAND;
			GetSprite()->SetAnimationIndex( m_CurrentState );
		}
	}

	if( m_CurrentState == ANIMATION_STATE_MOVE )
	{
		if( GetCurrentDir() == DIR_LEFT )
		{
			GetSprite()->SetScale( 1.0f , 1.0f , D3DXVECTOR2( GetWidth() / 2.0f , GetHeight() / 2.0f ) );
			moveX = - GetXVelocity() * deltaTime;
		}

		if( GetCurrentDir() == DIR_RIGHT )
		{
			GetSprite()->SetScale( -1.0f , 1.0f , D3DXVECTOR2( GetWidth() / 2.0f , GetHeight() / 2.0f ) );
			moveX = GetXVelocity() * deltaTime;
		}

		if( GetXposition() <= moveRange.fMinRange )
				SetDirection( DIR_RIGHT );

		if( GetXposition() >= moveRange.fMaxRange )
				SetDirection( DIR_LEFT );

		Move( moveX , 0 );

		if( collisionCallbacks( this , m_CallBack[CALLBACK_TYPE_COLLISION].pContext ) )
		{
			Move( -moveX , 0 );
			if( GetCurrentDir() == DIR_LEFT )
				SetDirection( DIR_RIGHT );
			else
				SetDirection( DIR_LEFT );
		}
	}


	UpdateHurt( deltaTime );

	GetSprite()->Update( deltaTime );
}
